package com.ch.boundarycallbackdemo.viewmodel

import android.os.AsyncTask
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.ch.boundarycallbackdemo.bean.Data
import com.ch.boundarycallbackdemo.bean.StateBean
import com.ch.boundarycallbackdemo.db.NewsBoundCallBack
import com.ch.boundarycallbackdemo.db.NewsDataBase

class NewsViewModel : ViewModel() {
    var newsList: LiveData<PagedList<Data>> = LivePagedListBuilder(
        NewsDataBase.INSTALL.getNewsDao().getDataList(),
        NewsBoundCallBack.PAGE_SIZE
    ).setBoundaryCallback(NewsBoundCallBack(this))
        .build()

    val stateBean: MutableLiveData<StateBean> = MutableLiveData<StateBean>()
    private var _stateBean = stateBean;

    fun setStateBean(bean: StateBean) {
        _stateBean.postValue(bean)
    }

    fun refresh() {
        AsyncTask.execute(Runnable {
            NewsDataBase.INSTALL.getNewsDao().clearData()
        })
    }

}